changed to stdint
added error handling