Remed and cleaned up code