lots of code improvements, error preventions, code beautification