refactoring for easier readability thanks to suggestions by dmcaulay