refactored the code a little bit