Add tests for applying source maps and refactor