Algorithm pretty much works, bugs with contexts are fixed