Proposing #310 again in fixed version

This is #310 with added fixes.