added some time time delays and cleaned up code