Various fixes and features

These patches fix bugs #619, #624 and #625; and implements enhancement #626.