Cleanup and improvement in the code