Modifications to the code to make it clearer and better structured