corrected bugs and endianess