Various code improvements (from skylot)