Cleanup and bug fixing