Optimizaztion needed. A version that works properly nevertheless.