various small fixes
reorder code