Updated the code to be clearer and work better.