Refactored and added some funcationality