WIP: refactoring and finalizing kde function