made the code simpler

here is some refactoring for you to consider.