Code cleanup and overall bug fixing.