Bug fixing and code cleaning