Code cleanup and fixing regression