Refactored window functions, fixed bug in Db calculation