bug fixes and improvements in readability