Implemented an alternative solution suggested by @fabpot.