Fixed & cleaned up the code