refactored the code to allow for a better output