Fix bug in dequeing
Copy remaining comments and credit original author