refactor toBytes to become utility method