Fixed functions, added casting, class definitions