speedup in logf and some error checking included.