Before changes
Rewritten the code to make it cleaner and better suiting