General improvments to code