final working version, needs some 'cleaning' and testing though