Further improvements to the code