Fixed occasional errors caused by hex values less than 5 characters

Cleaner way of handling value formatting too.