Clean up and improve the code