speed and stability improvements based on advice on julia-users list