code modified to work as TU Delft's code
version committed while waiting for input from TU Delft