Fixed some cast errors, improved options handling... could be much better thought.