Cleaned up handlecharref function

Encapsulated the function and fixed it to full functionality.