Move code which writes buffer into a case statement