code cleanup and fixing singleton implementation