Make the code a bit cleaner and added some comments