fixed an issue when going a level too deeper. also cleaned up the code a bit.