Cleaned the code and added database load optimization