Bug fixes and some beautification