Implemented buffer management using the 'message_peek' flag

Also adds some tests.