Misc fixes, functions cleanup