Added better exception handling for invalid buffer