added queueing functionality and cleaned up code