non-tested code. Added swi wrappers for sleep and time