Edge cases ficzed, works more like it should