Latest changes for task54. Most tricky logic remains to be implemented.