finally. a complete working version I think...