Cleaning up the code, minor bg fixes