Cleaned up traverse function and added comments