Properly handle buffer size

Fix issues with buffer size > metaInt, also simplified the code.