reworked code to be cleaner