Code improvement, bug fixes