General cleanup and bug fixing