Bug fixes + further code improvement