Made the code simpler with more comments