Small bug fixes and improvements to the code