he had displayed great courage in battle and a keen head for mathematics , which had won him promotion first to master's-mate , then to lieutenant , and even by a stroke of luck to post-rank , but he had never lost any of the coarseness of his background .