using c99 std, cleaned up code