fixed message order bug, cleaned up code some more