Minor bugfixes and clean the code.