Small bug fixes and code cleaning