Work in progress (refactored a bit)