Improve datetime handling. Fixes #32