Made bug fixes and code optimisations.