Fixed various small bugs and improved code style