Code cleaning; still not working perfectly