Factor out queue into its own class

@kyrylo will be proud of me :)