Fixed bugs when dealing with irregular datasets

Takes advantage of the reshape function cast() to deal with datasets
with missing data points