Fix timer with period

Using timer() with a period exposed quite some bugs. This adds a test and resolves them.